<?php

namespace mpend\components;

use Yii;
use mpend\filters\AppBearerAuth;
use common\models\app\AppToken;
use common\helpers\ArrayHelper;

/**
 * Controller is the base class for RESTful API controller classes.
 *
 * @property integer $uid
 * @property AppToken $token
 * @property mixed $user
 * @author emhome <emhome@163.com>
 * @since 1.0
 */
class Controller extends \common\rest\Controller {

    /**
     * @var array This Application's Serializer Configures.
     */
    public $authenticator = [
        'class' => AppBearerAuth::class,
    ];

    /**
     * @inheritdoc
     */
    protected function getCrosFilter() {
        return [
            'class' => \common\filters\Cors::class,
        ];
    }

    /**
     * 获取请求中加密数据信息
     * @return array
     */
    protected function getRequestEncryptData() {
        $params = Yii::$app->request->post();
        $ms = ArrayHelper::remove($params, 'ms');
        $attributes = $params;
        if ($ms) {
            /* @var $security \common\base\Security */
            $security = Yii::$app->security;
            $decryptData = $security->decryptDataByKey($ms);
            if ($decryptData !== false) {
                parse_str($decryptData, $output);
                $attributes = ArrayHelper::merge($attributes, $output);
            } else {
//            foreach ($params as $k => $v) {
//                $attributes[$k] = $security->decryptDataByKey($v);
//            }
            }
        }
        return $attributes;
    }

}
